package com.reqresp; /**
 * @author wzm
 * @create 2023-09-20 10:09
 * project_name:javaweb2308
 */

import org.apache.commons.io.IOUtils;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;

@WebServlet("/rs003")
public class RespServlet03 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }


    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //返回一个图片给浏览器
        //1.获取字节输出流
        ServletOutputStream os = response.getOutputStream();
        //2.从磁盘读取一个文件
        FileInputStream fis = new FileInputStream("e://a.png");

        //3.将输入流复制（读）到输出流写到浏览器即可
      /*  byte[] buff = new byte[1024];
        int len = 0;
        while((len =fis.read(buff)) != -1){
            //读到多少写多少
           os.write(buff,0,len);
        }

        //4.关闭资源
        fis.close();*/

        //使用工具进行流的复制 commons-io
        IOUtils.copy(fis,os);
        fis.close();



    }

}
